package com.tronsis.distribution;

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.os.StrictMode;
import android.support.multidex.MultiDex;

import com.lzy.imagepicker.ImagePicker;
import com.tencent.bugly.crashreport.CrashReport;
import com.tronsis.distribution.bean.RegionBean;
import com.tronsis.distribution.util.EmergencyHandler;
import com.tronsis.distribution.util.GlideImageLoader;
import com.tronsis.distribution.util.JsonUtil;
import com.tronsis.library.util.NotificationUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by 16172 on 2018/6/22.
 */

public class SysApplication extends Application {

    public static Context context;
    private static List<RegionBean> mRegionBeanList = new ArrayList<>();


    @Override
    public void onCreate() {
        super.onCreate();
        SysApplication.context = getApplicationContext();
        NotificationUtil.createNotificationChannel(this);
        EmergencyHandler.init(Environment.getExternalStorageDirectory().getPath()+"/01error");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }

        initRegionData();

        CrashReport.initCrashReport(getApplicationContext(), "22efad4ab7", false);

        ImagePicker.getInstance().setImageLoader(new GlideImageLoader());
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    public static Context getAppContext() {
        return SysApplication.context;
    }

    private void initRegionData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    mRegionBeanList = JsonUtil.getRegionList(SysApplication.getAppContext());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    public static List<RegionBean> getRegionList() {
        if (mRegionBeanList == null || mRegionBeanList.isEmpty()) {
            mRegionBeanList = JsonUtil.getRegionList(SysApplication.getAppContext());
        }

       return mRegionBeanList;
    }
}
